package net.xconcepts.stockdata.model;

import java.util.Collection;
import java.util.HashMap;

import net.xconcepts.stockdata.model.analysis.symbol.Symbol;

/**
 * Contains information regarding current holdings in cash and stocks
 * @author Steven
 *
 */
public class Portfolio {

	/** amount of cash in wallet */
	private double mCash;
	/** Symbol, shares */
	private HashMap<Symbol, Integer> mStocks = new HashMap<Symbol, Integer>();
	
	public Portfolio() {
		
	}
	
	/**
	 * 
	 * @param startingCash
	 */
	public Portfolio(double startingCash) {
		addCash(startingCash);
	}
	
	public double getCash() {
		return mCash;
	}
	
	/**
	 * 
	 * @param amount can be negative
	 */
	public void addCash(double amount) {
		mCash += amount;
	}
	
	public int getNumShares(Symbol symbol) {
		return mStocks.containsKey(symbol) ? mStocks.get(symbol) : 0;
	}
	
	public Collection<Symbol> getOwnedSymbols() {
		return mStocks.keySet();
	}
	
	/**
	 * returns the new amount of stock owned. Throws invalid transaction exception
	 * if error
	 * @param symbol
	 * @param amount can be negative
	 * @return
	 */
	public int addShares(Symbol symbol, int amount) {
		int newAmount;
		if (mStocks.containsKey(symbol)) {
			newAmount = mStocks.get(symbol) + amount;
		} else {
			newAmount = amount;
		}
		
		if(newAmount < 0) throw new InvalidTransactionException();
		
		mStocks.put(symbol, newAmount);
		
		return newAmount;
	}
	
	public class InvalidTransactionException extends RuntimeException {
		static final long serialVersionUID = -3066395707391335841L;
		
	}
}
